﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ApiBetaSeries.Factories;

namespace ApiBetaSeries.Services
{
    public class PlanningServices : ApiBetaSeries.Services.IPlanningServices
    {
        #region singleton
        private static PlanningServices m_planningServices = null;
        private PlanningServices() { }
        public static PlanningServices GetInstance()
        {
            if (m_planningServices == null)
            {
                m_planningServices = new PlanningServices();
            }
            return m_planningServices;
        }
        #endregion

        /// <summary>
        /// Get all episodes from the general planning
        /// </summary>
        /// <returns>list of the last episode on the general planning</returns>
        [ApiBetaSeries.Version.ApiMethod(true, "planning/general")]
        public List<IEpisode> GetPlanning()
        {
            List<IEpisode> list = new List<IEpisode>();

            XmlDocument doc = Utils.UtilsXML.GetUrl("/planning/general.xml");
            XmlElement xe = doc["root"]["planning"];

            if (xe == null)
            {
                throw new BetaSeriesException(1003, "Invalid action.");
            }
            foreach (XmlElement e in xe.GetElementsByTagName("episode"))
            {
                IEpisode episode = EpisodeFactory.CreateEpisode(e);
                list.Add(episode);
            }
            return list;
        }

        /// <summary>
        /// Get the planning of the identified member
        /// </summary>
        /// <param name="token">token of the member</param>
        /// <param name="unseen">show only the unseen episodes</param>
        /// <returns>list of episodes</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "planning/member")]
        public List<IEpisode> GetPlanning(string token, bool unseen = false)
        {
            IUser user = UserFactory.CreateUser();
            user.Token = token;
            return user.GetPlanning(unseen);
        }

        /// <summary>
        /// Get the planning of a member
        /// </summary>
        /// <param name="username">username of member</param>
        /// <param name="unseen">only the unseen episodes</param>
        /// <returns>list of episodes</returns>
        [ApiBetaSeries.Version.ApiMethod(true, "planning/member")]
        public List<IEpisode> GetPlanningOfMember(string username, bool unseen = false)
        {
            List<IEpisode> list = new List<IEpisode>();
            XmlDocument doc = Utils.UtilsXML.GetUrl("/planning/member/" + username + ".xml", (unseen ? "&view=unseen" : ""));
            XmlElement xe = doc["root"]["planning"];

            if (xe == null)
            {
                throw new BetaSeriesException(1003, "Invalid action.");
            }
            foreach (XmlElement e in xe.GetElementsByTagName("episode"))
            {
                IEpisode episode = EpisodeFactory.CreateEpisode(e);
                list.Add(episode);
            }
            return list;
        }
    }
}
